package cn.doitedu.bench.main;

import cn.doitedu.datagen.beans.Event;
import cn.doitedu.datagen.beans.EventResources;
import cn.doitedu.datagen.beans.User;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.io.*;
import java.util.*;

/***
 * @author hunter.d
 * @qq 657270652
 * @wx haitao-duan
 * @date 2021/12/15
 **/
public class GenApplog {
    public static void main(String[] args) throws Exception {
        int userNumber = Integer.parseInt(args[0]);
        Date date = DateUtils.parseDate(args[1], "yyyy-MM-dd_HH:mm:ss");
        String datePath = DateFormatUtils.format(date, "yyyy-MM-dd");
        long time1 = date.getTime();
        Date dateEnd = DateUtils.truncate(DateUtils.addDays(date, 1), Calendar.DATE);
        long time2 = dateEnd.getTime();

        boolean isNotBatch = Integer.parseInt(args[2]) == 1 ? true : false;

        String userFilePath = args[3];
        String outputPath = args[4];

        boolean isLogMode = Integer.parseInt(args[5]) == 1 ? true : false;

        BufferedWriter bw = new BufferedWriter(new FileWriter(outputPath+"/app.access.log."+datePath));

        int num = 0;
        while (num < userNumber+1001 ) {
            List<User> users = getUsers(userFilePath,1000);
            num += users.size();
            for(int i=0;i<users.size();i++) {
                User user = users.get(RandomUtils.nextInt(0, users.size()));
                getOneUserSessionLog(user, time1, time2, userFilePath, isNotBatch,bw,isLogMode);
                if (isNotBatch) Thread.sleep(200);
            }
        }
        bw.close();
    }

    public static void getOneUserSessionLog(User user, long time1, long time2, String userFilePath, boolean isNotBatch, BufferedWriter bw,boolean isLogMode) throws Exception {
        Event beginEvent = EventResources.getBeginEvent();
        user.setEventid(beginEvent.getEventId());
        user.setProperties(beginEvent.getProperties());
        user.setTimestamp(time1 + RandomUtils.nextInt(2000, 20000));


        // 模拟匿名
        if(RandomUtils.nextInt(1,50) % 9 ==0) user.setAccount("");
        //System.out.println(JSON.toJSONString(user));
        bw.write(JSON.toJSONString(user));
        bw.newLine();
        if(isLogMode) bw.flush();

        if (isNotBatch) Thread.sleep(200);
        user.setSessionid(RandomStringUtils.randomAlphabetic(8).toLowerCase());
        for (int i = 0; i < RandomUtils.nextInt(0, 100); i++) {
            Event event = EventResources.getEvent();
            user.setEventid(event.getEventId());
            user.setProperties(event.getProperties());
            user.setTimestamp(user.getTimestamp() + RandomUtils.nextInt(2000, 20000));

            //System.out.println(JSON.toJSONString(user));
            bw.write(JSON.toJSONString(user));
            bw.newLine();
            if(isLogMode) bw.flush();
            if (isNotBatch) Thread.sleep(500);
        }


    }

    public static List<User> getUsers(String userFilePath,int batchSize) throws Exception {
        ArrayList<User> users = new ArrayList<User>();
        File file = new File(userFilePath);
        RandomAccessFile r = new RandomAccessFile(file, "r");
        r.seek(getPos(file));
        r.readLine();

        String line = null;
        for (int i = 0; i < batchSize; i++) {
            line = r.readLine();
            if (line == null) {
                r.seek(getPos(file));
            }

            line = new String(line.getBytes("ISO-8859-1"));
            User user = JSON.parseObject(line, User.class);
            users.add(user);
        }
        r.close();

        return users;
    }

    public static long getPos(File file) {
        long pos = 500L * RandomUtils.nextLong(0, 9999000);
        while (pos + 2000 > file.length()) {
            pos = 500 * RandomUtils.nextLong(0, 9999000);
        }

        return pos;
    }


}
